package com.ccl.iot.dialogs;

import android.app.ProgressDialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ccl.iot.R;
import com.ccl.iot.device.IOTBLir;
import com.ccl.iot.device.IOTSubBLir;
import com.ccl.iot.ir.BlirConst;
import com.ccl.iot.publics.Messager;
import com.ccl.iot.publics.UIUtils;
import com.ccl.iot.views.SelectBrandView;
import com.hzy.tvmao.KookongSDK;
import com.hzy.tvmao.interf.IRequestResult;
import com.hzy.tvmao.ir.Device;
import com.kookong.app.data.BrandList;

import java.util.List;

public class AddSubBLirDialog extends CustomDialog {

    private GridView mGvSelectAppliance;
    private int[] mAppliancesName;
    private String[] mAppliancesType;
    private OnApplianceSelectListener mListener;
    private int[] mAppliancesImg;
    private EditText mEtName;

    public interface OnApplianceSelectListener {
        public IOTSubBLir onApplianceSelect(String type, String name);

        public IOTBLir getBlirDev();
    }

    public AddSubBLirDialog(Context context, OnApplianceSelectListener listener) {
        super(context, R.style.DialogTheme);
        mListener = listener;
        initView();
        initData();
    }

    @Override
    public View getView() {
        return View.inflate(getContext(), R.layout.dialog_selector_appliance, null);
    }

    private void initView() {
        mEtName = (EditText) findViewById(R.id.et_blir_name);
        mGvSelectAppliance = (GridView) findViewById(R.id.gv_select_appliance);
    }

    private void initData() {
        mAppliancesName = new int[]{R.string.appliance_tv, R.string.appliance_player, R.string.appliance_stb, R.string.appliance_projector, R.string.Air, R.string.appliance_fan,
                R.string.appliance_smart_box, R.string.appliance_amp, R.string.appliance_air_cleaner,R.string.appliance_water_heater,  R.string.custom};
        mAppliancesType = new String[]{BlirConst.IR_SUBDEV_TYPE_TV, BlirConst.IR_SUBDEV_TYPE_PLAYER,
                BlirConst.IR_SUBDEV_TYPE_STB, BlirConst.IR_SUBDEV_TYPE_PROJECTOR, BlirConst.IR_SUBDEV_TYPE_AIRCONDITION,
                BlirConst.IR_SUBDEV_TYPE_FAN, BlirConst.IR_SUBDEV_TYPE_BOX, BlirConst.IR_SUBDEV_TYPE_AMPLIFIER,
                BlirConst.IR_SUBDEV_TYPE_AIRPURIFIER, BlirConst.IR_SUBDEV_TYPE_WATER_HEATER, BlirConst.IR_SUBDEV_TYPE_OTHER};
        mAppliancesImg = new int[]{R.drawable.icon_tv_open, R.drawable.icon_dvd_open, R.drawable.icon_set_box_open,
                R.drawable.icon_projector_open, R.drawable.icon_air_open, R.drawable.icon_fan_default,
                R.drawable.icon_network_boxr_default, R.drawable.icon_attack_release_default, R.drawable.icon_air_clear_default,
                R.drawable.icon_heater_default, R.drawable.icon_customize};
        ApplianceAdapter adapter = new ApplianceAdapter();
        mGvSelectAppliance.setAdapter(adapter);
        mGvSelectAppliance.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String name = mEtName.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    name = getContext().getResources().getString(mAppliancesName[position]);
                }
                //                    mListener.onApplianceSelect(mAppliancesType[position], name);

                switch (position) {
                    case 0:
                        showSelectBrandDialog(Device.TV, name);
                        break;
                    case 1:
                        showSelectBrandDialog(Device.DVD, name);
                        break;
                    case 2:
                        SelectOperatorDialog dialog = new SelectOperatorDialog(getContext());
                        dialog.show();
                        dialog.setInitData(name, mListener);
                        break;
                    case 3:
                        showSelectBrandDialog(Device.PRO, name);
                        break;
                    case 4:
                        showSelectBrandDialog(Device.AC, name);
                        break;
                    case 5:
                        showSelectBrandDialog(Device.FAN, name);
                        break;
                    case 6:
                        showSelectBrandDialog(Device.BOX, name);
                        break;
                    case 7:
                        showSelectBrandDialog(Device.PA, name);
                        break;
                    case 8:
                        showSelectBrandDialog(Device.AIR_CLEANER, name);
                        break;
                    case 9:
                        showSelectBrandDialog(Device.WATER_HEATER, name);
                        break;
                    case 10:
                        mListener.onApplianceSelect(mAppliancesType[position], name);
                        break;
                    case 11:

                        break;
                    case 12:

                        break;
                }

                cancel();
            }
        });
    }

    private void showSelectBrandDialog(final int type, final String devName) {
        final ProgressDialog progressDialog = new ProgressDialog(getContext(), ProgressDialog.THEME_DEVICE_DEFAULT_LIGHT);
        progressDialog.show();
        KookongSDK.getBrandListFromNet(type, new IRequestResult<BrandList>() {

            @Override
            public void onSuccess(String msg, BrandList result) {
                List<BrandList.Brand> stbs = result.brandList;

                progressDialog.cancel();
                final HDCustomDialog dialog = new HDCustomDialog(getContext(), R.style.device_dialog_style_notitle);
                SelectBrandView brandView = new SelectBrandView(getContext());
                brandView.setBrands(stbs, type, new ModifyIconDialog.OnSelectChangeListener() {
                    @Override
                    public void onSelectChange() {
                        dialog.cancel();
                    }
                }, devName, mListener);

                dialog.setContentView(getContext().getResources().getString(R.string.control_select), brandView);
                dialog.show();
            }

            @Override
            public void onFail(Integer errorCode, String msg) {
                progressDialog.cancel();
                Messager.Show(msg);
                Log.e("getBrandListFromNet", "msg: " + msg + ", errorCode: " + errorCode);
            }
        });
    }

    class ApplianceAdapter extends BaseAdapter {
        private int mColor;
        private int paddingSize = UIUtils.dp2px(10);
        private int imgHeight = UIUtils.dp2px(80);

        @Override
        public int getCount() {
            mColor = getContext().getResources().getColor(R.color.air_text_color);
            return mAppliancesName.length;
        }

        @Override
        public Object getItem(int position) {
            return mAppliancesName[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                LinearLayout linearLayout = new LinearLayout(getContext());
                linearLayout.setPadding(paddingSize, paddingSize, paddingSize, paddingSize);
                linearLayout.setOrientation(LinearLayout.VERTICAL);

                ImageView iv = new ImageView(getContext());
                linearLayout.addView(iv, ViewGroup.LayoutParams.MATCH_PARENT, imgHeight);

                TextView tv = new TextView(getContext());
                tv.setGravity(Gravity.CENTER);
                tv.setTextColor(mColor);
                linearLayout.addView(tv, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                linearLayout.setBackgroundResource(R.drawable.selector_back_btn);
                convertView = linearLayout;
                holder = new ViewHolder(iv, tv);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv.setText(mAppliancesName[position]);
            holder.iv.setImageResource(mAppliancesImg[position]);

            return convertView;
        }

        class ViewHolder {
            ImageView iv;
            TextView tv;

            public ViewHolder(ImageView iv, TextView tv) {
                this.tv = tv;
                this.iv = iv;
            }
        }
    }

}
